home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / demos / OpenGL / walker / walkviewer.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  13KB  |  493 lines

  1. /*
  2.  * walkviewer.c [from agviewer.c  (version 1.0)]
  3.  *
  4.  * AGV: a glut viewer. Routines for viewing a 3d scene w/ glut
  5.  *
  6.  * See agv_example.c and agviewer.h comments within for more info.
  7.  *
  8.  * I welcome any feedback or improved versions!
  9.  *
  10.  * Philip Winston - 4/11/95
  11.  * pwinston@hmc.edu
  12.  * http://www.cs.hmc.edu/people/pwinston
  13.  */
  14.  
  15. #include <GL/gl.h>
  16. #include <GL/glu.h>
  17. #include <GL/glut.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <math.h>
  21.  
  22. #include "walkviewer.h"
  23.  
  24. /***************************************************************/
  25. /************************** SETTINGS ***************************/
  26. /***************************************************************/
  27.  
  28.    /* Initial polar movement settings */
  29. #define INIT_POLAR_AZ  0.0
  30. #define INIT_POLAR_EL 30.0
  31. #define INIT_DIST      3.0
  32. #define INIT_AZ_SPIN   0.5
  33. #define INIT_EL_SPIN   0.0
  34.  
  35.   /* Initial flying movement settings */
  36. #define INIT_EX        0.0
  37. #define INIT_EY       -2.0
  38. #define INIT_EZ       -2.0
  39. #define INIT_MOVE     0.01
  40. #define MINMOVE      0.001    
  41.  
  42.   /* Start in this mode */
  43. #define INIT_MODE   POLAR   
  44.  
  45.   /* Controls:  */
  46.  
  47.   /* map 0-9 to an EyeMove value when number key is hit in FLYING mode */
  48. #define SPEEDFUNCTION(x) ((x)*(x)*0.001)  
  49.  
  50.   /* Multiply EyeMove by (1+-MOVEFRACTION) when +/- hit in FLYING mode */
  51. #define MOVEFRACTION 0.25   
  52.  
  53.   /* What to multiply number of pixels mouse moved by to get rotation amount */
  54. #define EL_SENS   0.5
  55. #define AZ_SENS   0.5
  56.  
  57.   /* What to multiply number of pixels mouse moved by for movement amounts */
  58. #define DIST_SENS 0.01
  59. #define E_SENS    0.01
  60.  
  61.   /* Minimum spin to allow in polar (lower forced to zero) */
  62. #define MIN_AZSPIN 0.1
  63. #define MIN_ELSPIN 0.1
  64.  
  65.   /* Factors used in computing dAz and dEl (which determine AzSpin, ElSpin) */
  66. #define SLOW_DAZ 0.90
  67. #define SLOW_DEL 0.90
  68. #define PREV_DAZ 0.80
  69. #define PREV_DEL 0.80
  70. #define CUR_DAZ  0.20
  71. #define CUR_DEL  0.20
  72.  
  73. /***************************************************************/
  74. /************************** GLOBALS ****************************/
  75. /***************************************************************/
  76.  
  77. int     MoveMode = INIT_MODE;  /* FLYING or POLAR mode? */
  78.  
  79. GLfloat Ex = INIT_EX,             /* flying parameters */
  80.         Ey = INIT_EY,
  81.         Ez = INIT_EZ,
  82.         EyeMove = INIT_MOVE,     
  83.  
  84.         EyeDist = INIT_DIST,      /* polar params */
  85.         AzSpin  = INIT_AZ_SPIN,
  86.         ElSpin  = INIT_EL_SPIN,
  87.  
  88.         EyeAz = INIT_POLAR_AZ,    /* used by both */
  89.         EyeEl = INIT_POLAR_EL;
  90.  
  91. int agvMoving;    /* Currently moving?  */
  92.  
  93. int downx, downy,   /* for tracking mouse position */
  94.     lastx, lasty,
  95.     downb = -1;     /* and button status */
  96.                         
  97. GLfloat downDist, downEl, downAz, /* for saving state of things */
  98.         downEx, downEy, downEz,   /* when button is pressed */
  99.         downEyeMove;                
  100.  
  101. GLfloat dAz, dEl, lastAz, lastEl;  /* to calculate spinning w/ polar motion */
  102. int     AdjustingAzEl = 0;
  103.  
  104. int AllowIdle, RedisplayWindow; 
  105.    /* If AllowIdle is 1 it means AGV will install its own idle which
  106.     * will update the viewpoint as needed and send glutPostRedisplay() to the
  107.     * window RedisplayWindow which was set in agvInit().  AllowIdle of 0
  108.     * means AGV won't install an idle funciton, and something like
  109.     * "if (agvMoving) agvMove()" should exist at the end of the running
  110.     * idle function.
  111.     */
  112.  
  113. #define PI            3.14159265358979323846
  114. #define MAX(x,y) (((x) > (y)) ? (x) : (y))
  115. #define TORAD(x) ((PI/180.0)*(x))
  116. #define TODEG(x) ((180.0/PI)*(x))
  117.  
  118. /***************************************************************/
  119. /************************ PROTOTYPES ***************************/
  120. /***************************************************************/
  121.  
  122.   /*
  123.    * these are functions meant for internal use only
  124.    * the other prototypes are in agviewer.h
  125.    */
  126.  
  127. void PolarLookFrom(GLfloat dist, GLfloat elevation, GLfloat azimuth);
  128. void FlyLookFrom(GLfloat x, GLfloat y, GLfloat z, GLfloat az, GLfloat el);
  129. int  ConstrainEl(void);
  130. void MoveOn(int v);
  131. void SetMove(float newmove);
  132. void normalize(GLfloat v[3]);
  133. void ncrossprod(float v1[3], float v2[3], float cp[3]);
  134.  
  135.  
  136. /***************************************************************/
  137. /************************ agvInit ******************************/
  138. /***************************************************************/
  139.  
  140. void agvInit(int window)
  141. {
  142.   glutMouseFunc(agvHandleButton);
  143.   glutMotionFunc(agvHandleMotion);
  144.   glutKeyboardFunc(agvHandleKeys);
  145.   RedisplayWindow = glutGetWindow();
  146.   agvSetAllowIdle(window);
  147. }
  148.  
  149. /***************************************************************/
  150. /************************ VIEWPOINT STUFF **********************/
  151. /***************************************************************/
  152.  
  153.   /*
  154.    * viewing transformation modified from page 90 of red book
  155.    */
  156. void PolarLookFrom(GLfloat dist, GLfloat elevation, GLfloat azimuth)
  157. {
  158.   glTranslatef(0, 0, -dist);
  159.   glRotatef(elevation, 1, 0, 0);
  160.   glRotatef(azimuth, 0, 1, 0);
  161.  
  162. }
  163.  
  164.   /*
  165.    * I took the idea of tracking eye position in absolute
  166.    * coords and direction looking in Polar form from denis
  167.    */
  168. void FlyLookFrom(GLfloat x, GLfloat y, GLfloat z, GLfloat az, GLfloat el)
  169. {
  170.   float lookat[3], perp[3], up[3];
  171.  
  172.   lookat[0] = sin(TORAD(az))*cos(TORAD(el));
  173.   lookat[1] = sin(TORAD(el));
  174.   lookat[2] = -cos(TORAD(az))*cos(TORAD(el));
  175.   normalize(lookat);
  176.   perp[0] = lookat[2];
  177.   perp[1] = 0;
  178.   perp[2] = -lookat[0];
  179.   normalize(perp);
  180.   ncrossprod(lookat, perp, up);
  181.   gluLookAt(x, y, z,
  182.             x+lookat[0], y+lookat[1], z+lookat[2],
  183.             up[0], up[1], up[2]);
  184. }
  185.  
  186.   /*
  187.    * Call viewing transformation based on movement mode
  188.    */
  189. void agvViewTransform(void)
  190.   switch (MoveMode) {
  191.     case FLYING:
  192.       FlyLookFrom(Ex, Ey, Ez, EyeAz, EyeEl);
  193.       break;
  194.     case POLAR:
  195.       PolarLookFrom(EyeDist, EyeEl, EyeAz);
  196.       break;
  197.     }
  198. }
  199.  
  200.   /*
  201.    * keep them vertical; I think this makes a lot of things easier, 
  202.    * but maybe it wouldn't be too hard to adapt things to let you go
  203.    * upside down
  204.    */
  205. int ConstrainEl(void)
  206. {
  207.   if (EyeEl <= -90) {
  208.     EyeEl = -89.99;
  209.     return 1;
  210.   } else if (EyeEl >= 90) {
  211.     EyeEl = 89.99;
  212.     return 1;
  213.   }
  214.   return 0;
  215. }
  216.  
  217.  /*
  218.   * Idle Function - moves eyeposition
  219.   */
  220. void agvMove(void)
  221. {
  222.  
  223.   switch (MoveMode)  {
  224.     case FLYING:
  225.       Ex += EyeMove*sin(TORAD(EyeAz))*cos(TORAD(EyeEl));
  226.       Ey += EyeMove*sin(TORAD(EyeEl));
  227.       Ez -= EyeMove*cos(TORAD(EyeAz))*cos(TORAD(EyeEl));
  228.       break;
  229.  
  230.     case POLAR:
  231.       EyeEl += ElSpin;
  232.       EyeAz += AzSpin;
  233.       if (ConstrainEl()) {  /* weird spin thing to make things look     */
  234.         ElSpin = -ElSpin;      /* look better when you are kept from going */
  235.                                /* upside down while spinning - Isn't great */
  236.         if (fabs(ElSpin) > fabs(AzSpin))
  237.           AzSpin = fabs(ElSpin) * ((AzSpin > 0) ? 1 : -1);
  238.       }
  239.       break;
  240.     }
  241.  
  242.   if (AdjustingAzEl) {
  243.     dAz *= SLOW_DAZ;
  244.     dEl *= SLOW_DEL;
  245.   }
  246.  
  247.   if (AllowIdle) {
  248.     glutSetWindow(RedisplayWindow);
  249.     glutPostRedisplay();
  250.   }
  251. }
  252.  
  253.  
  254.   /*
  255.    * Don't install agvMove as idle unless we will be updating the view
  256.    * and we've been given a RedisplayWindow
  257.    */
  258. void MoveOn(int v)
  259. {
  260.   if (v && ((MoveMode == FLYING && EyeMove != 0) ||
  261.              (MoveMode == POLAR &&
  262.              (AzSpin != 0 || ElSpin != 0 || AdjustingAzEl)))) {
  263.     agvMoving = 1;
  264.     if (AllowIdle)
  265.       glutIdleFunc(agvMove);
  266.   } else {
  267.     agvMoving = 0;
  268.     if (AllowIdle)
  269.       glutIdleFunc(NULL);
  270.   }
  271. }
  272.  
  273.   /*
  274.    * set new redisplay window.  If <= 0 it means we are not to install
  275.    * an idle function and will rely on whoever does install one to 
  276.    * put statement like "if (agvMoving) agvMove();" at end of it
  277.    */
  278. void agvSetAllowIdle(int allowidle)
  279. {
  280.   if ((AllowIdle = allowidle))
  281.     MoveOn(1);
  282. }
  283.  
  284.  
  285.   /*
  286.    * when moving to flying we stay in the same spot, moving to polar we
  287.    * reset since we have to be looking at the origin (though a pivot from
  288.    * current position to look at origin might be cooler)
  289.    */
  290. void agvSwitchMoveMode(int move)
  291. {
  292.   switch (move) {
  293.     case FLYING:
  294.       Ex    = -EyeDist*sin(TORAD(EyeAz))*cos(TORAD(EyeEl));
  295.       Ey    =  EyeDist*sin(TORAD(EyeEl));
  296.       Ez    =  EyeDist*(cos(TORAD(EyeAz))*cos(TORAD(EyeEl)));
  297.       EyeAz =  EyeAz;
  298.       EyeEl = -EyeEl;
  299.       EyeMove = INIT_MOVE;
  300.       break;
  301.     case POLAR:
  302.       EyeDist = INIT_DIST;
  303.       EyeAz   = INIT_POLAR_AZ;
  304.       EyeEl   = INIT_POLAR_EL;
  305.       AzSpin  = INIT_AZ_SPIN;
  306.       ElSpin  = INIT_EL_SPIN;
  307.       break;
  308.     }
  309.   MoveMode = move;
  310.   MoveOn(1);
  311.   glutPostRedisplay();
  312. }
  313.  
  314. /***************************************************************/
  315. /*******************    MOUSE HANDLING   ***********************/
  316. /***************************************************************/
  317.  
  318. void agvHandleButton(int button, int state, int x, int y)
  319. {
  320.  if (state == GLUT_DOWN && downb == -1) {  
  321.     lastx = downx = x;
  322.     lasty = downy = y;
  323.     downb = button;    
  324.  
  325.     switch (button) {
  326.       case GLUT_LEFT_BUTTON:
  327.         lastEl = downEl = EyeEl;
  328.         lastAz = downAz = EyeAz;
  329.         AzSpin = ElSpin = dAz = dEl = 0;
  330.         AdjustingAzEl = 1;
  331.     MoveOn(1);
  332.         break;
  333.  
  334.       case GLUT_MIDDLE_BUTTON:
  335.         downDist = EyeDist;
  336.     downEx = Ex;
  337.     downEy = Ey;
  338.     downEz = Ez;
  339.     downEyeMove = EyeMove;
  340.     EyeMove = 0;
  341.     }
  342.  
  343.   } else if (state == GLUT_UP && button == downb) {
  344.  
  345.     downb = -1;
  346.  
  347.     switch (button) {
  348.       case GLUT_LEFT_BUTTON:
  349.         if (MoveMode != FLYING) {
  350.       AzSpin =  -dAz;
  351.       if (AzSpin < MIN_AZSPIN && AzSpin > -MIN_AZSPIN)
  352.         AzSpin = 0;    
  353.       ElSpin = -dEl;
  354.       if (ElSpin < MIN_ELSPIN && ElSpin > -MIN_ELSPIN)
  355.         ElSpin = 0; 
  356.     }
  357.         AdjustingAzEl = 0;
  358.         MoveOn(1);
  359.     break;
  360.  
  361.       case GLUT_MIDDLE_BUTTON:
  362.     EyeMove = downEyeMove;
  363.       }
  364.   }
  365. }
  366.  
  367.  /*
  368.   * change EyeEl and EyeAz and position when mouse is moved w/ button down
  369.   */
  370. void agvHandleMotion(int x, int y)
  371. {
  372.   int deltax = x - downx, deltay = y - downy;
  373.  
  374.   switch (downb) {
  375.     case GLUT_LEFT_BUTTON:
  376.       EyeEl  = downEl + EL_SENS * ((MoveMode == FLYING) ? -deltay : deltay);
  377.       ConstrainEl();
  378.       EyeAz  = downAz + AZ_SENS * deltax;
  379.       dAz    = PREV_DAZ*dAz + CUR_DAZ*(lastAz - EyeAz);
  380.       dEl    = PREV_DEL*dEl + CUR_DEL*(lastEl - EyeEl);
  381.       lastAz = EyeAz;
  382.       lastEl = EyeEl;
  383.       break;
  384.     case GLUT_MIDDLE_BUTTON:
  385.         EyeDist = downDist + DIST_SENS*deltay;
  386.         Ex = downEx - E_SENS*deltay*sin(TORAD(EyeAz))*cos(TORAD(EyeEl));
  387.         Ey = downEy - E_SENS*deltay*sin(TORAD(EyeEl));
  388.         Ez = downEz + E_SENS*deltay*cos(TORAD(EyeAz))*cos(TORAD(EyeEl));
  389.       break;
  390.   }
  391.   glutPostRedisplay();
  392. }
  393.  
  394. /***************************************************************/
  395. /********************* KEYBOARD HANDLING ***********************/
  396. /***************************************************************/
  397.  
  398.   /*
  399.    * set EyeMove (current speed) for FLYING mode
  400.    */
  401. void SetMove(float newmove)
  402. {
  403.   if (newmove > MINMOVE) {
  404.     EyeMove = newmove;
  405.     MoveOn(1);
  406.   } else {
  407.     EyeMove = 0;
  408.     MoveOn(0);
  409.   }
  410. }
  411.  
  412.   /*
  413.    * 0->9 set speed, +/- adjust current speed  -- in FLYING mode
  414.    */
  415. void agvHandleKeys(unsigned char key, int x, int y)
  416. {
  417.   if (MoveMode != FLYING)
  418.     return;
  419.  
  420.   if (key >= '0' && key <= '9')
  421.     SetMove(SPEEDFUNCTION((key-'0')));
  422.   else
  423.     switch(key) {
  424.       case '+':  
  425.         if (EyeMove == 0)
  426.           SetMove(MINMOVE);
  427.          else
  428.       SetMove(EyeMove *= (1 + MOVEFRACTION));
  429.         break;
  430.       case '-':
  431.     SetMove(EyeMove *= (1 - MOVEFRACTION));
  432.         break;
  433.     }
  434. }
  435.  
  436. /***************************************************************/
  437. /*********************** VECTOR STUFF **************************/
  438. /***************************************************************/
  439.  
  440.   /* normalizes v */
  441. void normalize(GLfloat v[3])
  442. {
  443.   GLfloat d = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
  444.  
  445.   if (d == 0)
  446.     fprintf(stderr, "Zero length vector in normalize\n");
  447.   else
  448.     v[0] /= d; v[1] /= d; v[2] /= d;
  449. }
  450.  
  451.   /* calculates a normalized crossproduct to v1, v2 */
  452. void ncrossprod(float v1[3], float v2[3], float cp[3])
  453. {
  454.   cp[0] = v1[1]*v2[2] - v1[2]*v2[1];
  455.   cp[1] = v1[2]*v2[0] - v1[0]*v2[2];
  456.   cp[2] = v1[0]*v2[1] - v1[1]*v2[0];
  457.   normalize(cp);
  458. }
  459.  
  460. /***************************************************************/
  461. /**************************** AXES *****************************/
  462. /***************************************************************/
  463.  
  464.  
  465.   /* draw axes -- was helpful to debug/design things */
  466. void agvMakeAxesList(int displaylistnum)
  467. {
  468.   int i,j;
  469.   GLfloat axes_ambuse[] =   { 0.5, 0.0, 0.0, 1.0 };
  470.   glNewList(displaylistnum, GL_COMPILE);
  471.   glPushAttrib(GL_LIGHTING_BIT);
  472.     glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, axes_ambuse);
  473.     glBegin(GL_LINES);
  474.       glVertex3f(15, 0, 0); glVertex3f(-15, 0, 0);
  475.       glVertex3f(0, 15, 0); glVertex3f(0, -15, 0);
  476.       glVertex3f(0, 0, 15); glVertex3f(0, 0, -15);
  477.     glEnd();
  478.     for (i = 0; i < 3; i++) {
  479.       glPushMatrix();
  480.         glTranslatef(-10*(i==0), -10*(i==1), -10*(i==2));
  481.         for (j = 0; j < 21; j++) {
  482.           glutSolidCube(0.1);
  483.           glTranslatef(i==0, i==1, i==2);
  484.     }
  485.       glPopMatrix();
  486.     }
  487.   glPopAttrib();
  488.   glEndList();  
  489. }
  490.  
  491.  
  492.